package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DishService dishService;
    @GetMapping(value = "/list",name = "查询所有菜品")
    public ResultInfo list(Long categoryId,Integer status){
        //使用key，在redis中看看有没有
        List<Dish> dishes = (List<Dish>) redisTemplate.opsForValue().get("dish:" + categoryId);
        if (CollectionUtil.isNotEmpty(dishes)){
            return ResultInfo.success(dishes);
        }
        //当程序走到此处，表示redis中没有缓存的菜品，此时要调用业务层查询
        dishes = dishService.findByIdNamelike(categoryId, null);
        //把查询结果存入redis中
        redisTemplate.opsForValue().set("dish:"+categoryId,dishes);
        return ResultInfo.success(dishes);
    }
}
